package com.agua.enturno.vista.paginas.adm.agendas.jornadas;

import com.agua.enturno.excepciones.ExcepcionDeReglaDelNegocio;
import com.agua.enturno.servicios.AgendaService;
import com.agua.enturno.servicios.JornadaLaboralHabitualService;
import com.agua.enturno.util.JodaTimeUtils;
import com.agua.enturno.vista.BasePage;
import com.agua.enturno.vista.componentes.ConfirmLink;
import com.agua.enturno.vista.dto.JornadaLaboralHabitualDTO;
import com.agua.enturno.vista.eventos.EliminacionDeJornadaHabitualEvent;
import com.agua.enturno.vista.modelos.*;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.event.Broadcast;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.joda.time.Duration;
import org.joda.time.LocalTime;


public class JornadaHabitualPanel extends Panel {

	private static final long serialVersionUID = 1L;

	@SpringBean
	private AgendaService agendaService;
	@SpringBean
	private JornadaLaboralHabitualService jornadaHabitualService;
	
	public JornadaHabitualPanel(String id, IModel<JornadaLaboralHabitualDTO> model) {
		super(id, model);
		this.setOutputMarkupId(true);
		
		@SuppressWarnings("rawtypes")
		Form form = new Form("jornada-habitual-form");
		
    	form.add(new AjaxSubmitLink("guardar-jornada") {
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				guardarJornada();
				info("La jornada se guardó satisfactoriamente");
				target.add(((BasePage) this.getPage()).getFeedbackPanel());
			}

			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				target.add(((BasePage) this.getPage()).getFeedbackPanel());
			}

		});
		
		form.add(new DropDownChoice<String>("dia-desde",  new TextDayOfWeekModel(new PropertyModel<Integer>(this, "jornadaHabitual.diaYHoraDeInicio.dayOfWeek")), JodaTimeUtils.getInstance().getDaysOfWeek()));
		form.add(new TextField<String>     ("hora-desde", new LocalTimeModel(new PropertyModel<LocalTime>(this, "jornadaHabitual.diaYHoraDeInicio.time"))));
		form.add(new DropDownChoice<String>("dia-hasta",  new TextDayOfWeekModel(new PropertyModel<Integer>(this, "jornadaHabitual.diaYHoraDeFin.dayOfWeek")), JodaTimeUtils.getInstance().getDaysOfWeek()));
		form.add(new TextField<String>     ("hora-hasta", new LocalTimeModel(new PropertyModel<LocalTime>(this, "jornadaHabitual.diaYHoraDeFin.time"))));
		
		form.add(new TextField<Integer>("duracion-turno-dias",    new DaysInNumberModel(new PropertyModel<Duration>(this, "jornadaHabitual.duracionDeLosTurnos")), Integer.class));
		form.add(new TextField<Integer>("duracion-turno-horas",   new HoursInNumberModel(new PropertyModel<Duration>(this, "jornadaHabitual.duracionDeLosTurnos")), Integer.class));
		form.add(new TextField<Integer>("duracion-turno-minutos", new MinutesInNumberModel(new PropertyModel<Duration>(this, "jornadaHabitual.duracionDeLosTurnos")), Integer.class));
		form.add(new TextField<Integer>("vacantes-por-turno",     new PropertyModel<Integer>(this, "jornadaHabitual.vacantesPorTurno"), Integer.class));
		
		form.add(new ConfirmLink("eliminar-jornada", "¿Realmente desea eliminar la jornada laboral?") {
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				eliminarJornada();
			}
		});
		
		add(form);
	}

	private void guardarJornada() {

		try {
			
			// TODO Pedirle al adm. de agenda que ingrese el motivo por el cual modifica la jornada
			JornadaLaboralHabitualDTO jornadaHabitual = getJornadaHabitual();
			
			if (jornadaHabitual.getId() == null)
				jornadaHabitual.setId(agendaService.agregarJornadaLaboralHabitual(jornadaHabitual.getAgendaId(), jornadaHabitual));
			else
				jornadaHabitualService.modificarJornada(jornadaHabitual.getId(), jornadaHabitual, "reestructuración en las jornadas laborales");
			
		} catch (ExcepcionDeReglaDelNegocio e) {
			error(e.getMessage());
		}
	}
	
	private void eliminarJornada() {

		try {
			// TODO Pedirle al adm. de agenda que ingrese el motivo por el cual modifica la jornada
			JornadaLaboralHabitualDTO jornadaHabitual = getJornadaHabitual();
			
			if (jornadaHabitual.getId() != null)
				agendaService.quitarJornadaLaboralHabitual(jornadaHabitual.getAgendaId(), jornadaHabitual.getId(), "reestructuración en las jornadas laborales");
			
			send(getPage(), Broadcast.BREADTH, new EliminacionDeJornadaHabitualEvent());
			
		} catch (ExcepcionDeReglaDelNegocio e) {
			error(e.getMessage());
		}
	}
	
	public JornadaLaboralHabitualDTO getJornadaHabitual() {
		return (JornadaLaboralHabitualDTO) getDefaultModel().getObject();
	}

}
